<?php
/*******************************
*  显示分页-- 2007.07.25 陈业华*
*******************************/
class Dpage  {
	var $tt,$per,$fp,$ep,$np,$ac,$tp,$sp,$cp,$f,$a,$pr;//tt--总数据,$per--每页数,fp--开始页,ep--结束页,np--选中页,tp--总页数,sp--显示页数,cp--选中样式
	public function __construct($total,$per_page='10',$now,$action='',$show=8,$color='current',$f='',$a='',$max_page_total="")
	{

		$this->tt = $total;
		$this->per = $per_page;
		$sum = ceil($total/$per_page);
		$this->tp = $sum = $sum>0?$sum:1;

		$this->tp = $sum;

		if($max_page_total > 0 && $sum > $max_page_total) {
			$this->tp = $max_page_total; //最大显示页数
		}

		$this->np = $now;

		if(empty($action))
		{
			if (!empty($_GET['sw_py']) && !empty($GLOBALS['pVars']['orignal_url']))
			{
				$url = $GLOBALS['pVars']['orignal_url'];
				if ($GLOBALS['pVars']['is_sort_kw'])
				{
					// 如果是分类关键词，则要在分类里面去掉关键词
					$url = str_replace($GLOBALS['pVars']['sw'],'',$url);
					$url = str_replace('.html','-s-'.$_GET['s'].'.html',$url);
				}
			}
			else
			$url = $_SERVER['REQUEST_URI'];

			$urlPares = parse_url($url);
			if(!empty($urlPares['path']))
			{
				$this->ac = preg_replace (array("'/p-\d+'i","'-p-\d+'si","'\.html$'si"), array('/','',''), $urlPares['path']);
				if(!empty($urlPares['query'])) $this->pr = '?'.$urlPares['query'];
			}
			else $this->ac = preg_replace (array("'/p-\d+'i","'-p-\d+'si","'\.html$'si"), array('/','',''), $_SERVER['REQUEST_URI']);
		}
		else $this->ac = $action;
		$this->sp = $show;
		$this->cp = $color;
		$this->f = $f;
		$this->a = $a;
	}

	function outputPagesTrade($type=0,$bp = ' ',$ep = ' ') {
		$this->analysePages();
		$rePageStr = '';
		if($type>=1){
			$this->np!=1 &&	$rePageStr .= $bp.'<a href="'.$this->setHref($this->np-1).'">上一页</a>'.$ep;
			$this->np!=$this->tp &&	$rePageStr .= $bp.'<a href="'.$this->setHref($this->np+1).'">下一页</a>'.$ep;
		}
		return $rePageStr;
	}

	function analysePages()//Analyse the output pages
	{
		if($this->tp<=$this->sp)
		{
			$this->fp=1;
			$this->ep=$this->tp;
		}
		else if($this->np<=ceil($this->sp/2))
		{
			$this->fp=1;
			$this->ep=$this->sp;
		}
		else if(($this->tp-$this->np)<ceil($this->sp/2))
		{
			$this->fp=$this->tp-$this->sp+1;
			$this->ep=$this->tp;
		}
		else
		{
			$this->fp=$this->np-ceil($this->sp/2)+1;
			$this->ep=$this->np+floor($this->sp/2);
		}
	}

	function setHref($i)
	{
		if($i == 1)
		$reStr = $this->f.(substr($this->ac,-1)=='/'?$this->ac:($this->ac.'.html')).$this->pr.$this->a;
		elseif (preg_match('/\/$/',$this->ac) || preg_match('/\/p-\d+\.html/',$this->ac)) // 商铺页面使用的分页方式
		$reStr = $this->f.$this->ac.'p-'.$i.'.html'.$this->pr.$this->a;
		else
		$reStr = $this->f.$this->ac.'-p-'.$i.'.html'.$this->pr.$this->a;
		return $reStr;
	}





	/*
	* 行业站的搜索分页
	*/

	function outputPages($type=0,$bp = ' ',$ep = ' ')
	{
		$this->analysePages();
		$rePageStr = '';
		if($type == 0){
			$rePageStr .= <<<EOL
<span>共<span style="color:red" id="total_p_num">{$this->tt}</span>条记录</span><span>&nbsp;{$this->per}条/页</span>
EOL;

}
if($this->np!=1) {
	$rePageStr .= $bp.'<a href="'.$this->setHref(1).'">首页</a>'.$ep;
	$rePageStr .= $bp.'<a href="'.$this->setHref($this->np-1).'">上一页</a>'.$ep;
}  else {
	$rePageStr .= $bp.'<a class="num_color" href="javascript:void(0)">首页</a>'.$ep;
	$rePageStr .= $bp.'<a class="num_color" href="javascript:void(0)">上一页</a>'.$ep;
}
if($this->fp!=1)
$rePageStr .= $bp.'<a href="'.$this->setHref(1).'">1</a>'.$ep;
$rePageStr .= $this->pagesnum($bp,$ep);
if($this->np!=$this->tp){
	$rePageStr .= $bp.'<a href="'.$this->setHref($this->np+1).'">下一页</a>'.$ep;
	$rePageStr .= $bp.'<a href="'.$this->setHref($this->tp).'">尾页</a>'.$ep;
}else{
	$rePageStr .= $bp.'<a class="num_color" href="javascript:void(0)">下一页</a>'.$ep;
	$rePageStr .= $bp.'<a class="num_color" href="javascript:void(0)">尾页</a>'.$ep;
}
if($type==0)
{
	$rePageStr .= $bp.'&nbsp;<input type="text" size=5 onkeydown="if(!((event.keyCode >= 46 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) && event.keyCode != 8){ if(event.preventDefault){event.preventDefault();}else{event.returnValue=false;}}"><input type="button" class="btn" value="跳转" onClick="if(this.previousSibling.value>'.$this->tp.') this.previousSibling.value='.$this->tp.';location.href=\''.$this->ac.'-p-\'+(this.previousSibling.value?this.previousSibling.value:\'1\')+\'.html'.$this->pr.'\'">'.$ep;
	return $rePageStr;
}else{
	return "<ul id='page_ul'>".$rePageStr."&nbsp;<span>共&nbsp;".$this->tp."&nbsp;页</span></ul>";
}
	}

	
	
	
	
	/*
	* 商铺分页
	*/

	function outputPagesShop($type='pc')
	{
		$this->analysePages();
		$rePageStr = '';
		if($type == "pc"){
			$rePageStr .= <<<EOL
<li>共<span style="color:red" id="total_p_num">{$this->tt}</span>条记录</span><span>&nbsp;{$this->per}条/页</li>
EOL;

}
if($this->np!=1) {
	$rePageStr .= '<li><a href="'.$this->setHref(1).'">首页</a></li>';
	$rePageStr .= '<li><a href="'.$this->setHref($this->np-1).'">上一页</a></li>';
}  else {
	$rePageStr .= '<li><a class="num_color" href="javascript:void(0)">首页</a></li>';
	$rePageStr .= '<li><a class="num_color" href="javascript:void(0)">上一页</a></li>';
}
if($this->fp!=1)
$rePageStr .= '<li><a href="'.$this->setHref(1).'">1</a></li>';

for($i=$this->fp;$i<=$this->ep;$i++)
{
	$thisp="";
	if($i == $this->np) $thisp = "class='active'";
$rePageStr .= '<li '.$thisp.'><a href="'.$this->setHref($i).'" '.$thisp.'>'.$i.'</a></li>';
			

}




if($this->np!=$this->tp){
	$rePageStr .= '<li><a href="'.$this->setHref($this->np+1).'">下一页</a></li>';
	$rePageStr .= '<li><a href="'.$this->setHref($this->tp).'">尾页</a></li>';
}else{
	$rePageStr .= '<li><a class="num_color" href="javascript:void(0)">下一页</a></li>';
	$rePageStr .= '<li><a class="num_color" href="javascript:void(0)">尾页</a></li>';
}
if($type=='pc')
{
	$rePageStr .= '<li>&nbsp;<input type="text" size=5 onkeydown="if(!((event.keyCode >= 46 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) && event.keyCode != 8){ if(event.preventDefault){event.preventDefault();}else{event.returnValue=false;}}"><input type="button" class="btn" value="跳转" onClick="if(this.previousSibling.value>'.$this->tp.') this.previousSibling.value='.$this->tp.';location.href=\''.$this->ac.'-p-\'+(this.previousSibling.value?this.previousSibling.value:\'1\')+\'.html'.$this->pr.'\'"></li>';
    return "<ul id='page_ul'>".$rePageStr."&nbsp;<li><a>共&nbsp;".$this->tp."&nbsp;页</a></li></ul>";
}else{
	
	return "<ul id='page_ul' class='pagination'>".$rePageStr."&nbsp;<li><a>共&nbsp;".$this->tp."&nbsp;页</a></li></ul>";
}
	}
	
	
	
	
	
	
	/*
	* 搜索分页
	* @author 张伟杰
	*/

	function pagesnum($bp='',$ep='')
	{
		$reStr = '';
		for($i=$this->fp;$i<=$this->ep;$i++)
		{
			if($i!=$this->np) {
				if($this->ep == $this->tp && $i== $this->ep){
					$reStr .= $bp.'<a class="endpage" href="'.$this->setHref($i).'">'.$i.'</a>'.$ep;
					continue;
				}else{
					$reStr .= $bp . '<a href="' . $this->setHref($i) . '">' . $i . '</a>' . $ep;
				}
			}
			else
			$reStr .= $this->setColor($i,$bp,$ep);
		}
		return $reStr;
	}

	function setColor($str, $bp, $ep)
	{
		if($this->cp == 'ico_page')
		return '<span class="ico_page"><a class="'.$this->cp.'">'.$str.'</a></span>';
		else
		return $bp.'<a class="'.$this->cp.'">'.$str.'</a>'.$ep;
	}

	/*
	* 前台列表页和搜索页分页和商铺
	* @$setarr array  分页显示配置
	* @author jackie
	
	* */

	public function outputFrontPages($setarr='')
	{
		
		$this->sp = 5;
		$this->cp = 'ico_page';
		$this->tp = $this->tp > 200 ? 200 : $this->tp;
		$rePageStr = '';
		$omit  = '<span class="no_border">...</span>';
		$this->analysePages();
		if($this->np!=1){
			$rePageStr .= '<span class="page_ico page_ico_l"><a href="'.$this->setHref($this->np-1).'">上一页</a></span>';
		}else{
			$rePageStr .= '<span class="page_ico page_ico_l_hover"><a href="javascript:;">上一页</a></span>';
		}
		if($this->fp!=1){
			$rePageStr .= '<span><a href="'.$this->setHref(1).'">1</a></span>';
		}
		if($this->fp >=3){
			$rePageStr .= $omit;
		}
		$rePageStr .= $this->pagesnum('<span>','</span>');

		if($this->np!=$this->tp){
			if($this->ep != $this->tp){
				$rePageStr .= $omit;
			}
			$rePageStr .= '<span class="page_ico page_ico_r"><a href="'.$this->setHref($this->np+1).'">下一页</a></span>';
		}else{
			$rePageStr .= '<span class="page_ico page_ico_r_hover"><a href="javascript:;">下一页</a></span>';
		}
		
		if($this->ac!='/')
		{
			$this->ac = $this->ac.'-';
		}
		
		if($setarr['total'] <> 'none') $rePageStr .= '<p class="page_p">&nbsp;共&nbsp;'.$this->tp.'&nbsp;页&nbsp;到';
		if($setarr['go'] <> 'none'){
			 $rePageStr .= '<span class="page_input_span"><input id="nextPageInput" style="width:44px;" class="input" type="text" maxlength="5" value="" onkeypress="return frontPage.keypress_gopage(event);">&nbsp;页
                        <span class="page_btn_span"><input type="button" class="btn" onClick="frontPage.gopage()" value="确定"></span></p>';
		$rePageStr .= <<<EOL
        <script>
        var frontPage= {
			//张伟杰 2015-02-10
		   page_input_c:function()
		   {
				if(navigator.userAgent.indexOf("MSIE")>0){

					document.getElementById('nextPageInput').attachEvent("onpropertychange",txChange);

					}else if(navigator.userAgent.indexOf("Firefox")>0){

					document.getElementById('nextPageInput').addEventListener("input",txChange,false);

					}

					function txChange(){

						var str_page = $('#nextPageInput').val();
						if(str_page){
							if(!/^\d+$/.test(str_page) || parseInt(str_page)>{$this->tp} || parseInt(str_page)<1)
							{
								$('#nextPageInput').val({$this->np});
								return;
							}
						}
					}

		   },
		  
            keypress_gopage : function(){
                var event = arguments[0] || window.event;
                var code = event.keyCode || event.charCode;
               
                if(code == 8) return true;
               
                if(code == 13){
                    frontPage.gopage();
                    return false;
                }
               
                if(event.ctrlKey && (code == 99 || code == 118)) return true;
								
               
                if(code<48 || code>57)return false;
                return true;
            },
            gopage:function(){
                var str_page = $('#nextPageInput').val();
                if(!str_page){
                    return;
                }
                var n = parseInt(str_page);
                if(n < 1){
                    n = 1;
                }else if(n > {$this->tp}){
                    n = {$this->tp};
                }
                location.href="{$this->ac}p-"+n+".html";
            }
        }
		frontPage.page_input_c();
        </script>
EOL;
}
		return $rePageStr;
}

/**
	 * 带问号的传统分页(跟outputPages函数一样)
	 * @author dora(邝达峰)
	 */

function questionMarkPages($type=0,$bp = ' ',$ep = ' ')
{
	$this->analysePages();
	$rePageStr = '';
	if($type>=1)
	{
		if($this->np!=1)
		$rePageStr .= $bp.'<a href="'.$this->setQuestionMarkHref($this->np-1).'" hideFocus="true" >上一页</a>'.$ep;
		if($this->fp!=1)
		$rePageStr .= $bp.'<a href="'.$this->setQuestionMarkHref(1).'" hideFocus="true" >1</a>'.$ep;
	}
	$rePageStr .= $this->questionMarkPagesnNum($bp,$ep);
	if($type>=1)
	{
		/*			if($this->ep!=$this->tp)
		$rePageStr .= $bp.'<a href="'.$this->setHref($this->tp).'">'.$this->tp.'</a>'.$ep;
		*/
		if($this->np!=$this->tp)
		$rePageStr .= $bp.'<a href="'.$this->setQuestionMarkHref($this->np+1).'" hideFocus="true" >下一页</a>'.$ep;
		else
		$rePageStr .= '';
	}
	if($type>=1)
	{
		//$rePageStr .= $bp.'&nbsp;<input type="text" size=5 onkeydown="if(!((event.keyCode >= 46 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) && event.keyCode != 8){ if(event.preventDefault){event.preventDefault();}else{event.returnValue=false;}}"><input type="button" class="btn" value="跳转" onClick="if(this.previousSibling.value>'.$this->tp.') this.previousSibling.value='.$this->tp.';location.href=\''.$this->ac.'\'+(this.previousSibling.value?this.previousSibling.value:\'1\')+\'.html'.$this->pr.'\'">'.$ep;
	}
	return $rePageStr;
}

/**设置带问号的传统分页
* @author dora(邝达峰)
*/
function setQuestionMarkHref($i)
{
	if($i == 1)
	$reStr = $this->f.$this->ac.'.html';
	elseif (preg_match('/\/$/',$this->ac) || preg_match('/\/p-\d+\.html/',$this->ac)) // 商铺页面使用的分页方式
	$reStr = $this->f.$this->ac.'.html?p='.$i;
	else
	$reStr = $this->f.$this->ac.'.html?p='.$i;
	return $reStr;
}

/**设置带问号的传统分页
* @author dora(邝达峰)
*/
function questionMarkPagesnNum($bp,$ep)
{
	$reStr = '';
	for($i=$this->fp;$i<=$this->ep;$i++)
	{
		if($i!=$this->np)
		$reStr .= $bp.'<a href="'.$this->setQuestionMarkHref($i).'" hideFocus="true" >'.$i.'</a>'.$ep;
		else
		$reStr .= $bp.$this->setColor($i).$ep;
	}
	return $reStr;
}

/***分页样式 lee 2014-09-24*
* @	str $type 默认1
* @ return str 返回分页样式,放在当前模板页的<style></style>中
**/
function pageCss($type='1'){
	if($type=='1'){
		return  <<<lee
                #page_ul{  margin:0 auto; padding:25px 0; text-align:center }
                #page_ul li {display:inline; padding:0;}
                #page_ul a {padding:0 10px; height:30px; line-height:30px; border:1px solid #ccc; display:inline-block; margin:0 3px; text-decoration:none}
                #page_ul li a:hover, #page_ul .current{ border:1px solid #ff9900; background:#ff9900; color:#fff;text-decoration:none }
                #page_ul .num_color{color:#ccc!important;border:1px solid #ccc!important; background:#fff!important;}
                #page .totalnum {}
lee;
}elseif($type=='2'){
	return <<<lee
                #page_ul{ border:0; margin:0; padding:0;width:400px; }
                #page_ul li { border:0; margin:0; padding:0; font-size:11px; list-style:none; /* savers */ float:left; }
                #page_ul a { border:solid 1px #9aafe5; margin-right:2px; }
                #page_ul .previous-off,#page_ul .next-off  { border:solid 1px #DEDEDE; color:#888888; display:block; float:left; font-weight:bold; margin-right:2px; padding:3px 4px; }
                #page_ul .next a,#page_ul .previous a { font-weight:bold; }	
                #page_ul .current { background:#2e6ab1; color:#FFFFFF; font-weight:bold; display:block; float:left; padding:4px 6px; /* savers */ margin-right:2px; }
                #page_ul a:link,#page_ul a:visited { color:#0e509e; display:block; float:left; padding:3px 6px; text-decoration:none; }
                #page_ul a:hover { border:solid 1px #0e509e; }
lee;
}elseif ($type == 'front'){
	return <<<lee
                #pages{
                    clear:both;
                    height:30px;
                    line-height:30px;
                    margin-top:20px;
                    color:#999999;
                    font-size:14px;
                }
                #pages a{
                    padding:4px 8px;
                    margin:10px 3px;
                    font-size:12px;
                    border:1px solid #DFDFDF;
                    background-color:#FFF;
                    color:#9d9d9d;
                    text-decoration:none;
                }
                #pages span{
                    font-size:14px;
                }
                #pages span.disabled{
                    padding:4px 8px;
                    margin:10px 3px;
                    font-size:12px;
                    border:1px solid #DFDFDF;
                    background-color:#FFF;
                    color:#DFDFDF;
                }
                #pages span.current{
                    padding:4px 8px;
                    margin:10px 3px;
                    font-size:12px;
                    border:1px solid #FF6600;
                    background-color:#FF6600;
                    color:#FFF;
                }
                #pages a:hover{
                    background-color:#FFEEE5;
                    border:1px solid #FF6600;
                }
                #pages span.pagenum{
                    font-size:12px;
                }
                #pages_btn_go {
                    background-clip: padding-box;
                    border: 1px solid #ededed;
                    border-radius: 2px;
                    cursor: pointer;
                    height: 21px;
                    line-height: 21px;
                    text-align: center;
                    width: 43px;
                    margin-left: 5px;
                }
                #pages_btn_go:hover {
                    border-color: #f40;
                    color: #f40;
                }
                #pages_btn_go_input{
                    width:42px;
                    height:16px;
                    text-align:center;
                    left:0px;
                    top:0px;
                    outline:none;
                    margin: 0 4px;
                }
lee;
}else{
	return <<<lee
                #page_ul{ border:0; margin:0; padding:0;width:400px; }
                #page_ul li { border:0; margin:0; padding:0; font-size:11px; list-style:none; /* savers */ float:left; }
                #page_ul a { border:solid 1px #DDDDDD; margin-right:2px; }
                #page_ul .previous-off,#page_ul .next-off { color:#666666; display:block; float:left; font-weight:bold; padding:3px 4px; }
                #page_ul .next a,#page_ul .previous a { font-weight:bold; border:solid 1px #FFFFFF; }	
                #page_ul .current { color:#ff0084; font-weight:bold; display:block; float:left; padding:4px 6px; }
                #page_ul a:link, 
                #page_ul a:visited { color:#0063e3; display:block; float:left; padding:3px 6px; text-decoration:none; }
                #page_ul a:hover { border:solid 1px #000; background:#0063DC; color:#FFF; }
       
lee;
}

}

/***ajax分页 lee 2013-12-24*
* @	str $ty 默认getPage js函数名
* @ arr $args js参数
* @ return arr 返回分页数组
**/
function ajaxPage($ty = 'getPage',$args =array()){
	$tt = $this->tt;
	$tpn = $this->tp;
	$list = $this->per;
	$page = $this->np;
	if($tt<=0 || $tpn<=1 || $tt<$list){
		$res['tt'] = $tt;
		$res['lpage'] = $list;
		$res['showmes'] = '<a href="javascript:;">当前第'.$page.'/'.($tpn<1? $tpn+1 : $tpn).'页</a>';
		$res['first']='<a href="javascript:;">[首页]</a>';
		$res['prev']='<a href="javascript:;">[上一页]</a>';
		$res['next']= '<a href="javascript:;">[下一页]</a>';
		$res['last']= '<a href="javascript:;">[尾页]</a>';
		return $res;
	}
	if(!empty($args)){
		$st =",'".implode("','",$args)."'";
	}else{
		$st ="";
	}
	$fpage = ($page-5>0)?$page-5:1;
	if($page+5<$tpn){
		$lpage = $page+5;
	}else{
		$lpage = $tpn;
		$fpage = $tpn - 9;
	}
	$lpage = ($fpage ==1)?10:$lpage;
	$res['tt'] = $tt;
	$res['lpage'] = $list;
	$res['showmes'] =  '<a href="javascript:;">当前第'.$page.'/'.$tpn.'页</a>';
	$res['first']= ($page==1) ? '<a href="javascript:;">[首页]</a>' : '<a href="javascript:;" onclick="'.$ty.'(\'1\''.$st.')">[首页]</a>';
	$res['prev']=($page>=2) ? '<a href="javascript:;" onclick="'.$ty.'(\''.($page-1).'\''.$st.')">[上一页]</a>' : '<a href="javascript:;">[上一页]</a>';
	for($i=(($fpage > 0)?$fpage : 1);$i<=$lpage;$i++){
		$res['list'][$i]= '<a href="javascript:;" onclick="'.$ty.'(\''.$i.'\''.$st.')">'.$i.'</a>';
	}
	$res['next']= ($page != $tpn) ? '<a href="javascript:;" onclick="'.$ty.'(\''.($page+1).'\''.$st.')">[下一页]</a>' : '<a href="javascript:;">[下一页]</a>';
	$res['last']= ($page==$tpn) ? '<a href="javascript:;">[尾页]</a>' : '<a href="javascript:;" onclick="'.$ty.'(\''.$tpn.'\''.$st.')">[尾页]</a>';
	//$res['output']  = $res['showmes'].$res['first'].$res['prev'];
	$res['output']  = $res['prev'];
	if(!empty($res['list'])){
		foreach($res['list'] as $v){
			$str .= $v;
		}
		$res['output'] .=$str;
	}
	$res['output'] .= $res['next'];

	return $res;
}

}
?>